package ui 
{
	import data.*;
	import events.*;
	import flash.text.TextField;
	import ui.button.GCButton;
	import ui.popup.GCUpgradePopup;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import org.flixel.FlxG;
	
	/**
	 * A component that will display all of the upgrades that a ship
	 * has. Also includes a list of upgrades that can be added.
	 */
	public class GCShipUpgradesArea extends Sprite 
	{
		private static var SPRITE_OFFSET:int = 10;
		private static var COMPONENT_WIDTH:int = 300;
		private static var COMPONENT_HEIGHT:int = 100;
		
		/*
		 * Keep a list of buttons that represent empty upgrade slots
		 * the player can fill and the upgrades they have already applied
		 * to this ship.
		 * 
		 * In addition, remember which button that we clicked.
		 */
		private var _upgradeSlotOnFocus:GCUpgradeSprite;
		private var _upgradeSlots:Vector.<GCUpgradeSprite>;
		private var _numUpgradeSlots:int;
		
		/*
		 * Offsets of where to put the upgrades
		 */
		private var _upgradeSlotX:int;
		private var _upgradeSlotY:int;
		
		/*
		 * The 'container' holding the ship upgrade slots
		 */
		private var _buttonStrip:Sprite;
		
		private var _toLeftButton:GCButton;
		private var _toRightButton:GCButton;
		
		/*
		 * A container for menu when a player clicks on the upgrade
		 * slot.
		 */
		private var _upgradePopup:GCUpgradePopup;
		
		public function GCShipUpgradesArea() 
		{			
			_upgradeSlotX = 0;
			_upgradeSlotY = 10;
			_buttonStrip = new Sprite();
			_toLeftButton = new GCButton(onLeftDown);
			_toRightButton = new GCButton(onRightDown);
			_upgradePopup = new GCUpgradePopup();
			
			_upgradeSlotOnFocus = null;
			_upgradeSlots = new Vector.<GCUpgradeSprite>();
			
			init();
		}
		
		/**
		 * Function to get the upgrade bar to display correctly
		 */
		public function addNewSlot(slotId:uint, itemId:uint=GCItemData.ITEM_NULL):void
		{
			var upgrade:GCUpgradeSprite = new GCUpgradeSprite(slotId,itemId, _upgradeSlotX, _upgradeSlotY);
			upgrade.addEventListener(GCEvent.UPGRADE_SELECTED_EVENT, onSlotClicked);
			_upgradeSlotX += upgrade.width + SPRITE_OFFSET;
			_upgradeSlots.push(upgrade);
			
			_buttonStrip.addChild(upgrade);
		}
		
		/**
		 * Function to clean up the area of all components
		 */
		public function cleanArea():void
		{
			_upgradeSlotOnFocus = null;
			
			for each(var component:GCUpgradeSprite in _upgradeSlots)
			{
				component.removeEventListener(GCEvent.UPGRADE_SELECTED_EVENT, onSlotClicked);
				_buttonStrip.removeChild(component);
				
				while (component.numChildren > 0)
				{
					component.removeChildAt(0);
				}
			}
			
			_upgradeSlots = new Vector.<GCUpgradeSprite>();
			_upgradeSlotX = 0;
		}
		
		private function init():void
		{
			_upgradePopup.addEventListener(GCEvent.POPUP_CLOSED_EVENT, onPopupClosed);
			
			_toLeftButton.x = 0;
			_toLeftButton.loadGraphic(GCResources.left_arrow, 25, 35);
			
			var leftPadding:int = _toLeftButton.x + _toLeftButton.width;
			
			/*
			 * Adding a mask
			 */
			var mask:Shape = new Shape;
			mask.graphics.beginFill(0x000000);
			mask.graphics.drawRect(leftPadding, 0, COMPONENT_WIDTH, COMPONENT_HEIGHT + 15);
			addChild(mask);
			
			_buttonStrip.x = mask.x + leftPadding;
			_buttonStrip.mask = mask;
			
			_toRightButton.x = mask.width + leftPadding;
			_toRightButton.y = _toLeftButton.y;
			_toRightButton.loadGraphic(GCResources.right_arrow, 25, 35);

			this.addChild(_buttonStrip);
			this.addChild(_toLeftButton);
			this.addChild(_toRightButton);
			
			_toLeftButton.y = this.height / 2 - _toLeftButton.height / 2;
			_toRightButton.y = _toLeftButton.y;
		}
		
		/**
		 * Triggered when the player clicks on an already applied upgrade.
		 * They can choose to discard the upgrade.
		 * 
		 * @param	event
		 */
		private function onSlotClicked(event:GCEvent):void
		{	
			
			var slot:GCUpgradeSprite = event.currentTarget as GCUpgradeSprite;
			_upgradeSlotOnFocus = slot;

			GCPlayState.disableScreen(_upgradePopup);
			_upgradePopup.x = FlxG.width / 2 - _upgradePopup.width / 2;
			_upgradePopup.y = FlxG.height / 2 - _upgradePopup.height / 2;
			_upgradePopup.openPopup(_upgradeSlotOnFocus);
		}

		private function onLeftDown(event:MouseEvent):void
		{

			if (_buttonStrip.x + _buttonStrip.width > _buttonStrip.mask.width)
			{
				_buttonStrip.x -= 60;
			}

		}
		
		private function onRightDown(event:MouseEvent):void
		{
			_buttonStrip.x += 60;
			
			if (_buttonStrip.x > SPRITE_OFFSET * 3)
			{
				_buttonStrip.x = SPRITE_OFFSET * 3;
			}
		}
		
		private function onPopupClosed(event:GCUpgradeEvent):void
		{
			GCPlayState.enableScreen();
			var upgradeId:uint;
			var slotId:uint;
			
			var updateRealData:GCUpgradeEvent = new GCUpgradeEvent(GCEvent.UPGRADE_CHANGED_EVENT);
			updateRealData.upgradeAction = event.upgradeAction;
			
			if (event.upgradeAction == GCUpgradeEvent.ADD_UPGRADE_EVENT)
			{
				trace("Added an upgrade");
				
				// Add upgrade visually to UI
				upgradeId = event.upgradeItemId;
				slotId = event._upgradeComponent.upgradeSlotId;
				event._upgradeComponent.fillWithUpgrade(event.upgradeItemId);
				
				updateRealData.upgradeItemId = upgradeId;
				updateRealData.upgradeSlotId = slotId;
				
				dispatchEvent(updateRealData);
			}
			else if (event.upgradeAction == GCUpgradeEvent.DISCARD_UPGRADE_EVENT)
			{
				trace("Removed an upgrade");
				
				// Remove upgrade visaully from UI
				upgradeId = event.upgradeItemId;
				slotId = event._upgradeComponent.upgradeSlotId;
				event._upgradeComponent.removeUpgrade();
				
				updateRealData.upgradeSlotId = slotId;
				
				dispatchEvent(updateRealData);
			}
			else
			{
				trace("Closed popup");
			}
		}
	}

}